<?php
defined ( 'BASEPATH' ) or exit ( 'No direct script access allowed' );
require_once (dirname ( __FILE__ ) . "/../../PHPExcel/PHPExcel/Reader/Excel5.php");
class Passport extends CI_Controller {
	public function __construct() {
		parent::__construct ();
	}
	public function test9(){
		$this->load->model ( 'order_model' );
		$this->load->model ( 'orderdetail_model' );
		$orders = $this->order_model->find_by_id (961);
		foreach($orders as $item){
			$result = $this->orderdetail_model->find_by_order ($item['id']);
			if(count($result)!=9){//是否订全
				echo $item['id'].'-';
			}
		}
		echo "OK";
	}
	public function test8(){
		$this->load->model ( 'cookbook_model' );
		$this->load->model ( 'order_model' );
		$cookbook = $this->cookbook_model->find_by_id (75);
		foreach($cookbook as $item){
			$result = $this->order_model->sum_by_date_school_type_grade ($item['date'], $item['schoolid'],$item['type'],-1,$item['productid']);
			if(!empty($result)){
				$data = array (
					'stock' => $item['daylim']-$result['sum']
				);
				$this->cookbook_model->update ($item['id'],$data);
			}
		}
		echo "OK";
	}
	public function test7(){//订单详情为空的订单
		$i=0;
		$this->load->model ( 'order_model' );
		$this->load->model ( 'orderdetail_model' );
		$orders=$this->order_model->find_del_orders();
		foreach($orders as $order){
			$this->orderdetail_model->delete_by_order($order['id']);
			$i++;
		}
		echo "已删除".$i."条订单";
	}
	public function test6(){//订单详情为空的订单
		$this->load->model ( 'order_model' );
		$this->load->model ( 'orderdetail_model' );
		$orders=$this->order_model->find_all_orders();
		foreach($orders as $order){
			$temp=$this->orderdetail_model->find_by_order($order['id']);
			if(empty($temp)){
				echo $order['id']."-";
			}
		}
		echo "OK";
	}
	public function test5(){//取消下单后不能下单的问题
		$this->load->model ( 'user_model' );
		$this->load->model ( 'order_model' );
		$users=$this->user_model->find_order_users();
		foreach($users as $user){
			$temp=$this->order_model->find_by_user($user['id']);
			if(empty($temp)){
				echo $user['id'];
				/*
				$newuser = array (
					'order_state' =>0
				);
				$this->user_model->update ($user['id'],$newuser);*/
			}
		}
		echo "OK";
	}
	public function test(){
		$password="280108";
		echo md5 ( $password . 'jishibao' );
	}
	public function test2(){
		$this->load->model ( 'code_model' );
		$this->load->model ( 'user_model' );
		$codes = $this->code_model->find_all_code ();
		foreach($codes as $code){
			$user = array (
				'no' => $code ['no']
			);
			$this->user_model->update ($code['userid'],$user);
		}
		echo "OK";
	}
	public function test3(){
		$localfile="C:\\Users\\77374\\Desktop\\20180105.xlsx";
		$this->load->model ( 'user_model' );
		$objReader = new PHPExcel_Reader_Excel2007();
		$objPHPExcel = $objReader->load($localfile);
		$sheet = $objPHPExcel->getSheet(0);
		$row = $sheet->getHighestRow();
		for($i=2;$i<=$row;$i++){
			$name = $sheet->getCell("B".$i)->getValue();//姓名
			$user=$this->user_model->find_by_name ($name);
			if(empty($user)){
				echo $name."-";
				continue;
			}
			$newuser = array (
				'balance' => 1
			);
			$this->user_model->update ( $user['id'],$newuser );
		}
		echo "OK";
	}
	public function test4(){
		$localfile="D:\\yuxin.xlsx";
		$this->load->model ( 'user_model' );
		$objReader = new PHPExcel_Reader_Excel2007();
		$objPHPExcel = $objReader->load($localfile);
		$sheet = $objPHPExcel->getSheet(0);
		$row = $sheet->getHighestRow();
		for($i=2;$i<=$row;$i++){
			$sno = $sheet->getCell("D".$i)->getValue();//学号
			$user=$this->user_model->find_by_sno ($sno);
			if(empty($user)){
				echo $sno."-";
				continue;
			}
			/*
			$newuser = array (
				'balance' => 1
			);
			$this->user_model->update ( $user['id'],$newuser );*/
		}
		echo "OK";
	}
	/*
	 * 跳转到登陆页面
	 */
	public function index() {
		if ($this->session->userdata ( 'logged_in' )) {
			redirect ( 'order/index' );
		}
		$this->load->view ( 'login' );
	}
	/*
	 * 登陆验证
	 */
	public function login() {
		$username = $this->input->post ( 'username' );
		$password = $this->input->post ( 'password' );
		
		$this->load->model ( 'admin_model' );
		$admin = $this->admin_model->get_admin ( $username, md5($password.'jishibao'));
		if (!empty($admin) ){
			$data = array (
				'adminid' => $admin ['id'],
				'name' => $admin ['username'],
				'logged_in' => TRUE,
				'schoolid' => $admin ['schoolid']
			);
			$this->session->set_userdata ( $data );
			
			return_json(TRUE,'','登陆成功！'); 
		} else {
			return_json(FALSE,'','用户名或密码错误！'); 
		}
	}
	/*
	 * 注销
	 */
	public function logout(){
		$this->session->sess_destroy();
		redirect('passport/index');
	}
	/*
	 * 导入信息
	 */
	public function importProduct() {
		$file="C:\\Users\\77374\\Desktop\\test.xlsx";
		$this->load->model ( 'standard_model' );
		$objReader = new PHPExcel_Reader_Excel2007();
		$objPHPExcel = $objReader->load($file);
		$sheet = $objPHPExcel->getSheet(0);
		$row = $sheet->getHighestRow();
		for($i=2;$i<=$row;$i++){
			$data = array (
				'age' => $sheet->getCell("B".$i)->getValue(),
				'gender' => $sheet->getCell("C".$i)->getValue(),
				'nengliang' => $sheet->getCell("D".$i)->getValue(),
				'water' => $sheet->getCell("E".$i)->getValue(),
				'danbaizhi' => $sheet->getCell("F".$i)->getValue(),
				'zhifang' => $sheet->getCell("G".$i)->getValue(),
				'xianwei' => $sheet->getCell("H".$i)->getValue(),
				'tanshui' => $sheet->getCell("I".$i)->getValue(),
				'weiA' => $sheet->getCell("J".$i)->getValue(),
				'weiB1' => $sheet->getCell("K".$i)->getValue(),
				'weiB2' => $sheet->getCell("L".$i)->getValue(),
				'yansuan' => $sheet->getCell("M".$i)->getValue(),
				'weiE' => $sheet->getCell("N".$i)->getValue(),
				'na' => $sheet->getCell("O".$i)->getValue(),
				'ca' => $sheet->getCell("P".$i)->getValue(),
				'fe' => $sheet->getCell("Q".$i)->getValue(),
				'weiC' => $sheet->getCell("R".$i)->getValue(),
				'dangu' => $sheet->getCell("S".$i)->getValue()
			);
			$this->standard_model->create ( $data );
		}
		var_dump("导入成功");
		die();
    }
	/*
	 * 判断学生营养情况
	 */
	public function judgeNutrient() {
		$userid = $this->input->post ( 'userid' );
		$startdate = $this->input->post ( 'startdate' );
		$enddate = $this->input->post ( 'enddate' );
		$this->load->model ( 'user_model' );
		$this->load->model ( 'order_model' );
		$this->load->model ( 'standard_model' );
		$user=$this->user_model->load($userid);
		$day=floor((strtotime($enddate)-strtotime($startdate))/86400)+1;
		$userNutrient=$this->order_model->count_by_date_user ( $userid,$startdate,$enddate,$day);
		//var_dump($userNutrient);
		$standardNutrient=$this->standard_model->find_by_age ( $user['age'],$user['gender'] );//暂时不判断为空
		$score=0;
		$nutrients=array("tanshui","weiA","weiB1","weiB2","yansuan","weiE","na","ca","fe","weiC");
		foreach($nutrients as $nutrient){
			//var_dump($userNutrient[$nutrient]);
			if($userNutrient[$nutrient]>$standardNutrient[$nutrient]*0.4){
				$score+=10;
			}
		}
		var_dump($score);
		die();
	}
	/*
	 * 初始化密码
	 */
	public function updatePassword() {
		$this->load->model ( 'user_model' );
		$users=$this->user_model->find_all_user();
		foreach($users as $user){
			if(empty($user['password'])){
				$newuser = array (
					'password' => md5 (substr($user['sno'],-6) . 'jishibao' )
				);
				$this->user_model->update($user['id'],$newuser);
			}
		}
		echo "初始化密码成功！";
	}
}
